package com.android.jianboshi.ui.activity;

import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;

import com.android.baselibrary.ui.AbsBaseActivity;
import com.android.baselibrary.view.Comm_HeadView;
import com.android.jianboshi.R;
import com.android.jianboshi.manager.LanguageManager;
import com.android.jianboshi.utils.LocaleUtils;
import com.gyf.immersionbar.ImmersionBar;

import java.util.Locale;

public abstract class BaseActivity extends AbsBaseActivity {


    //重写字体缩放比例 api<25
    @Override
    public Resources getResources() {
        Resources res = super.getResources();
        if(LanguageManager.getCurrentLocale()!= Locale.CHINA ||LanguageManager.getCurrentLocale()!= Locale.ENGLISH ||LanguageManager.getCurrentLocale()!= Locale.JAPANESE)
        if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.N) {
            Configuration config = res.getConfiguration();
            config.fontScale = 0.8f;//设置正常字体大小的倍数
            res.updateConfiguration(config, res.getDisplayMetrics());
        }
        return res;
    }



    //重写字体缩放比例  api>25
    @Override
    protected void attachBaseContext(Context newBase) {
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.N && (LanguageManager.getCurrentLocale()!= Locale.CHINA ||LanguageManager.getCurrentLocale()!= Locale.ENGLISH ||LanguageManager.getCurrentLocale()!= Locale.JAPANESE)) {
            final Resources res = newBase.getResources();
            final Configuration config = res.getConfiguration();
            config.fontScale = 0.8f;//设置正常字体大小的倍数
            final Context newContext = newBase.createConfigurationContext(config);
            super.attachBaseContext(newContext);
        } else {
            super.attachBaseContext(newBase);
        }
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Comm_HeadView comm_title = findViewById(R.id.comm_title);
        if (comm_title != null) {
            comm_title.setLeftClickFinish(this);
        }
        //是否沉浸式
        if (isImmersion()) {
            if (getImmersionTitleBar() != null) {
                ImmersionBar.with(this).titleBar(getImmersionTitleBar());
            }
            ImmersionBar.with(this).statusBarDarkFont(false).navigationBarDarkIcon(false).init();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        LocaleUtils.changeAppLanguage(this, LanguageManager.getCurrentLocale());
    }

    protected boolean isImmersion() {
        return true;
    }

    protected View getImmersionTitleBar() {
        Comm_HeadView comm_title = findViewById(R.id.comm_title);
        return comm_title;
    }

//    @Subscribe(threadMode = ThreadMode.MAIN)
//    public void onEventMainThread(Event_Token_Failure event) {
//        if (this.getClass().getSimpleName().equals(MainActivity.class.getSimpleName())||
//                this.getClass().getSimpleName().equals(LoadingActivity.class.getSimpleName())) {
//            UserManager.logout((Activity) mContext);
//            UserManager.toLogin(this);
//            finish();
//        } else if(!this.getClass().getSimpleName().equals(LoginActivity.class.getSimpleName())){
//            finish();
//        }
//    }


}
